package hierarchy

import (
	"gitcode.com/G-YT/ANN/server/global"
	"gitcode.com/G-YT/ANN/server/model/common/request"
	"gitcode.com/G-YT/ANN/server/model/common/response"
	"gitcode.com/G-YT/ANN/server/model/hierarchy"
	hierarchyReq "gitcode.com/G-YT/ANN/server/model/hierarchy/request"
	"gitcode.com/G-YT/ANN/server/service"
	"gitcode.com/G-YT/ANN/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"strconv"
)

type FdArticleGraphicApi struct {
}

var fdArticleGraphicService = service.ServiceGroupApp.HierarchyServiceGroup.FdArticleGraphicService

// CreateFdArticleGraphic 創建FdArticleGraphic
// @Tags FdArticleGraphic
// @Summary 創建FdArticleGraphic
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body hierarchy.FdArticleGraphic true "創建FdArticleGraphic"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"獲取成功"}"
// @Router /fdArticleGraphic/createFdArticleGraphic [post]
func (fdArticleGraphicApi *FdArticleGraphicApi) CreateFdArticleGraphic(c *gin.Context) {
	var fdArticleGraphic hierarchy.FdArticleGraphic
	err := c.ShouldBindJSON(&fdArticleGraphic)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := fdArticleGraphicService.CreateFdArticleGraphic(&fdArticleGraphic); err != nil {
		global.GVA_LOG.Error("創建失敗!", zap.Error(err))
		response.FailWithMessage("創建失敗", c)
	} else {
		response.OkWithMessage("創建成功", c)
	}
}

// DeleteFdArticleGraphic 删除FdArticleGraphic
// @Tags FdArticleGraphic
// @Summary 删除FdArticleGraphic
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body hierarchy.FdArticleGraphic true "删除FdArticleGraphic"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /fdArticleGraphic/deleteFdArticleGraphic [delete]
func (fdArticleGraphicApi *FdArticleGraphicApi) DeleteFdArticleGraphic(c *gin.Context) {
	var fdArticleGraphic hierarchy.FdArticleGraphic
	err := c.ShouldBindJSON(&fdArticleGraphic)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := fdArticleGraphicService.DeleteFdArticleGraphic(fdArticleGraphic); err != nil {
		global.GVA_LOG.Error("删除失敗!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteFdArticleGraphicByIds 批量删除FdArticleGraphic
// @Tags FdArticleGraphic
// @Summary 批量删除FdArticleGraphic
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除FdArticleGraphic"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /fdArticleGraphic/deleteFdArticleGraphicByIds [delete]
func (fdArticleGraphicApi *FdArticleGraphicApi) DeleteFdArticleGraphicByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := fdArticleGraphicService.DeleteFdArticleGraphicByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失敗!", zap.Error(err))
		response.FailWithMessage("批量删除失敗", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateFdArticleGraphic 更新FdArticleGraphic
// @Tags FdArticleGraphic
// @Summary 更新FdArticleGraphic
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body hierarchy.FdArticleGraphic true "更新FdArticleGraphic"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /fdArticleGraphic/updateFdArticleGraphic [put]
func (fdArticleGraphicApi *FdArticleGraphicApi) UpdateFdArticleGraphic(c *gin.Context) {
	var fdArticleGraphic hierarchy.FdArticleGraphic
	err := c.ShouldBindJSON(&fdArticleGraphic)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := fdArticleGraphicService.UpdateFdArticleGraphic(fdArticleGraphic); err != nil {
		global.GVA_LOG.Error("更新失敗!", zap.Error(err))
		response.FailWithMessage("更新失敗", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindFdArticleGraphic 用id査詢FdArticleGraphic
// @Tags FdArticleGraphic
// @Summary 用id査詢FdArticleGraphic
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query hierarchy.FdArticleGraphic true "用id査詢FdArticleGraphic"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"査詢成功"}"
// @Router /fdArticleGraphic/findFdArticleGraphic [get]
func (fdArticleGraphicApi *FdArticleGraphicApi) FindFdArticleGraphic(c *gin.Context) {
	var fdArticleGraphic hierarchy.FdArticleGraphic
	err := c.ShouldBindQuery(&fdArticleGraphic)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	userID := utils.GetUserID(c)
	if refdArticleGraphic, err := fdArticleGraphicService.GetFdArticleGraphic(fdArticleGraphic.ID, userID); err != nil {
		global.GVA_LOG.Error("査詢失敗!", zap.Error(err))
		response.FailWithMessage("査詢失敗", c)
	} else {
		response.OkWithData(gin.H{"refdArticleGraphic": refdArticleGraphic}, c)
	}
}

// GetFdArticleGraphicList 分页獲取图文类型文章列表
// @Tags FdArticleGraphic
// @Summary 分页獲取FdArticleGraphic列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query hierarchyReq.FdArticleGraphicSearch true "分页獲取图文类型文章列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"獲取成功"}"
// @Router /fdArticleGraphic/getFdArticleGraphicList [get]
func (fdArticleGraphicApi *FdArticleGraphicApi) GetFdArticleGraphicList(c *gin.Context) {
	var pageInfo hierarchyReq.FdArticleGraphicSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := fdArticleGraphicService.GetFdArticleGraphicInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage("獲取失敗", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "獲取成功", c)
	}
}

// GetArticleListByLevelTwoIDs 根据二级ID集合獲取文章列表
// @Tags FdArticleGraphic
// @Summary 根据二级ID集合獲取文章列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query hierarchyReq.FdArticleGraphicSearch true "根据二级ID集合獲取文章列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"獲取成功"}"
// @Router /fdArticleGraphic/getArticleListByLevelTwoIDs [get]
func (fdArticleGraphicApi *FdArticleGraphicApi) GetArticleListByLevelTwoIDs(c *gin.Context) {
	var pageInfo hierarchyReq.ArticleSearchByLevelTwoIDs
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := fdArticleGraphicService.GetArticleListByLevelTwoIDs(pageInfo); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage("獲取失敗", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "獲取成功", c)
	}
}

// FdArticleLike 用id査詢FdArticleAudio
// @Tags FdArticleAudio
// @Summary 用id査詢FdArticleAudio
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"査詢成功"}"
// @Router /fdArticleAudio/like [get]
func (fdArticleGraphicApi *FdArticleGraphicApi) FdArticleLike(c *gin.Context) {
	id := c.Query("id")
	uid, _ := strconv.Atoi(id)
	userID := utils.GetUserID(c)
	if refdArticleAudio, err := fdArticleGraphicService.FdArticleLike(userID, uint(uid)); err != nil {
		global.GVA_LOG.Error("査詢失敗!", zap.Error(err))
		response.FailWithMessage("査詢失敗", c)
	} else {
		// 添加点赞积分相关
		err = UserService.AddPoints(userID, 1, 1)
		response.OkWithData(gin.H{"refdArticleAudio": refdArticleAudio}, c)
	}
}

// FdArticleCollect 收藏
// @Tags FdArticleGraphic
// @Summary 收藏
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"査詢成功"}"
// @Router /fdArticleGraphic/collect [get]
func (fdArticleGraphicApi *FdArticleGraphicApi) FdArticleCollect(c *gin.Context) {
	id := c.Query("id")
	uid, _ := strconv.Atoi(id)
	userID := utils.GetUserID(c)
	if refdArticleAudio, err := fdArticleGraphicService.FdArticleCollect(userID, uint(uid)); err != nil {
		global.GVA_LOG.Error("査詢失敗!", zap.Error(err))
		response.FailWithMessage("査詢失敗", c)
	} else {
		response.OkWithData(gin.H{"refdArticleAudio": refdArticleAudio}, c)
	}
}
